/* 
 * Copyright (c), nFractals Ltd. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.parade.distributed;

import java.io.Serializable;

import org.parade.Constant;
import org.parade.Parade;
import org.parade.driver.Driver;

public class Id implements Serializable{
	private static final long serialVersionUID = 6294251334546197916L;
	
	String idString;
	
	/**
	 * Create cluster wide unique id using default singleton driver
	 */
	public Id(){
		Driver driver = Parade.getDriver();
		idString = Constant.ID_PREFIX +driver.getClass().getCanonicalName()+"-GENERATED-ID_"+Long.toString(Parade.getDriver().generateUniqueId());
	}

	/**
	 * Create cluster wide unique id using given driver
	 * @param driver	driver
	 */
	public Id(Driver driver){
		idString = Constant.ID_PREFIX+driver.getClass().getCanonicalName()+"-GENERATED-ID_"+Long.toString(Parade.getDriver().generateUniqueId());
	}
	
	/**
	 * Create id from string
	 * @param idString	id string
	 */
	public Id(String idString){
		this.idString = Constant.ID_PREFIX+idString;
	}
	
	public String toString(){
		return idString;
	}
	
	public boolean equals(Object o){
		return toString().equals(o.toString());
	}
	
	public int hashCode(){
		return idString.hashCode();
	}
}
